﻿<lib:EditControl x:Class="MoPlus.SolutionBuilder.WpfClient.UserControls.Diagrams.DiagramControl"
			xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
			xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
			xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
			xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
			xmlns:lib="clr-namespace:MoPlus.SolutionBuilder.WpfClient.Library"
			xmlns:uent="clr-namespace:MoPlus.SolutionBuilder.WpfClient.UserControls.Entities"
			xmlns:udiag="clr-namespace:MoPlus.SolutionBuilder.WpfClient.UserControls.Diagrams"
			xmlns:usol="clr-namespace:MoPlus.SolutionBuilder.WpfClient.UserControls.Solutions"
			xmlns:vm="clr-namespace:MoPlus.ViewModel;assembly=MoPlus.ViewModel"
			xmlns:svm="clr-namespace:MoPlus.ViewModel.Solutions;assembly=MoPlus.ViewModel"
			xmlns:evm="clr-namespace:MoPlus.ViewModel.Entities;assembly=MoPlus.ViewModel"
			xmlns:dvm="clr-namespace:MoPlus.ViewModel.Diagrams;assembly=MoPlus.ViewModel"
			xmlns:wvm="clr-namespace:MoPlus.ViewModel.Workflows;assembly=MoPlus.ViewModel"
            xmlns:Settings="clr-namespace:MoPlus.SolutionBuilder.WpfClient.Library;assembly=MoPlus.SolutionBuilder.WpfClient"
			mc:Ignorable="d" 
		    Loaded="EditControl_Loaded"
			d:DesignHeight="300" d:DesignWidth="300">
	<lib:EditControl.CommandBindings>
		<CommandBinding Command="New" CanExecute="NewCanExecute" Executed="NewExecuted"></CommandBinding>
		<CommandBinding Command="Delete" CanExecute="DeleteCanExecute" Executed="DeleteExecuted"></CommandBinding>
		<CommandBinding Command="vm:CustomCommands.Close" CanExecute="CloseCanExecute" Executed="CloseExecuted"></CommandBinding>
	</lib:EditControl.CommandBindings>
	<lib:EditControl.Resources>
		<ResourceDictionary>
			<ResourceDictionary.MergedDictionaries>
				<ResourceDictionary Source="../../Resources/Theme.xaml" />
				<ResourceDictionary Source="../../Resources/DiagramTheme.xaml"/>
			</ResourceDictionary.MergedDictionaries>
			<BitmapImage x:Key="PinImage" UriSource="../../Resources/Images/pin.png" />
			<Style x:Key="IconImageStyleSmall" TargetType="Image">
				<Setter Property="MaxWidth" Value="16"/>
				<Setter Property="MaxHeight" Value="16"/>
				<Setter Property="Margin" Value="0,0,0,0"/>
			</Style>
			<Style TargetType="{x:Type udiag:DiagramControl}">
				<Style.Triggers>
					<Trigger Property="IsDragging" Value="True">
						<Setter Property="Cursor" Value="Pen" />
					</Trigger>
				</Style.Triggers>
			</Style>
			<vm:LabelHelper x:Key="labels" />
			<!-- diagram relationship -->
			<DataTemplate DataType="{x:Type dvm:DiagramRelationshipViewModel}">
				<lib:Connection>
					<lib:Connection.ContextMenu>
						<ContextMenu>
							<MenuItem Header="{Binding MenuLabelDelete}" Command="Delete"></MenuItem>
						</ContextMenu>
					</lib:Connection.ContextMenu>
				</lib:Connection>
			</DataTemplate>
			<!-- diagram entity -->
			<DataTemplate DataType="{x:Type dvm:DiagramEntityViewModel}">
				<lib:DesignerItem>
					<udiag:EntityDiagramControl>
						<udiag:EntityDiagramControl.ContextMenu>
							<ContextMenu>
								<MenuItem Header="{Binding MenuLabelRemove}" Command="vm:CustomCommands.Close"></MenuItem>
								<MenuItem Header="{Binding MenuLabelDelete}" Command="Delete"></MenuItem>
							</ContextMenu>
						</udiag:EntityDiagramControl.ContextMenu>
					</udiag:EntityDiagramControl>
				</lib:DesignerItem>
			</DataTemplate>
		</ResourceDictionary>
	</lib:EditControl.Resources>
	<DockPanel DataContextChanged="Grid_DataContextChanged">
		<StackPanel Name="diagramTools" Orientation="Horizontal" DockPanel.Dock="Left" HorizontalAlignment="Right" VerticalAlignment="Top">
			<StackPanel.LayoutTransform>
				<TransformGroup>
					<ScaleTransform ScaleX="{Binding Source={x:Static Settings:UserSettingsHelper.Instance}, Path=ControlSize, Mode=TwoWay}"
					                            ScaleY="{Binding Source={x:Static Settings:UserSettingsHelper.Instance}, Path=ControlSize, Mode=TwoWay}"/>
					<RotateTransform Angle="-90" />
				</TransformGroup>
			</StackPanel.LayoutTransform>
			<Button Name="btnZoomBox" Margin="0,0,10,0" Width="Auto" MouseEnter="btnZoomBox_MouseEnter" Click="btnZoomBox_Click" >
				<StackPanel Orientation="Horizontal">
					<TextBlock Text="{Binding Source={StaticResource labels}, Path=ZoomBoxHeader}" Margin="0,0,5,0" />
					<Image x:Name="ZoomBoxDocked" Style="{StaticResource IconImageStyleSmall}" Source="{StaticResource PinImage}" Visibility="Collapsed">
						<Image.LayoutTransform>
							<RotateTransform Angle="90" />
						</Image.LayoutTransform>
					</Image>
					<Image x:Name="ZoomBoxUndocked" Style="{StaticResource IconImageStyleSmall}" Source="{StaticResource PinImage}" Visibility="Collapsed" />
				</StackPanel>
			</Button>
			<Button Name="btnDiagramToolBox" Margin="0,0,10,0" Width="Auto" MouseEnter="btnDiagramToolBox_MouseEnter"  Click="btnDiagramToolBox_Click" >
				<StackPanel Orientation="Horizontal">
					<TextBlock Text="{Binding Source={StaticResource labels}, Path=DiagramToolBoxHeader}" Margin="0,0,5,0" />
					<Image x:Name="DiagramDocked" Style="{StaticResource IconImageStyleSmall}" Source="{StaticResource PinImage}" Visibility="Collapsed">
						<Image.LayoutTransform>
							<RotateTransform Angle="90" />
						</Image.LayoutTransform>
					</Image>
					<Image x:Name="DiagramUndocked" Style="{StaticResource IconImageStyleSmall}" Source="{StaticResource PinImage}" Visibility="Collapsed" />
				</StackPanel>
			</Button>
			<Button Name="btnPropertiesToolBox" Margin="0,0,10,0" Width="Auto" MouseEnter="btnPropertiesToolBox_MouseEnter"  Click="btnPropertiesToolBox_Click" >
				<StackPanel Orientation="Horizontal">
					<TextBlock Text="{Binding Source={StaticResource labels}, Path=PropertiesToolBoxHeader}" Margin="0,0,5,0" />
					<Image x:Name="PropertiesDocked" Style="{StaticResource IconImageStyleSmall}" Source="{StaticResource PinImage}" Visibility="Collapsed">
						<Image.LayoutTransform>
							<RotateTransform Angle="90" />
						</Image.LayoutTransform>
					</Image>
					<Image x:Name="PropertiesUndocked" Style="{StaticResource IconImageStyleSmall}" Source="{StaticResource PinImage}" Visibility="Collapsed" />
				</StackPanel>
			</Button>
			<Button Name="btnEntitiesToolBox" Margin="0" Width="Auto" MouseEnter="btnEntitiesToolBox_MouseEnter"  Click="btnEntitiesToolBox_Click" >
				<StackPanel Orientation="Horizontal">
					<TextBlock Text="{Binding Source={StaticResource labels}, Path=EntityNameHeader}" Margin="0,0,5,0" />
					<Image x:Name="EntitiesDocked" Style="{StaticResource IconImageStyleSmall}" Source="{StaticResource PinImage}" Visibility="Collapsed">
						<Image.LayoutTransform>
							<RotateTransform Angle="90" />
						</Image.LayoutTransform>
					</Image>
					<Image x:Name="EntitiesUndocked" Style="{StaticResource IconImageStyleSmall}" Source="{StaticResource PinImage}" Visibility="Collapsed" />
				</StackPanel>
			</Button>
		</StackPanel>
		<Grid Name="ParentGrid">
			<Grid Margin="2" Name="DockedGrid">
				<Grid.ColumnDefinitions>
					<ColumnDefinition Name="ToolBoxColumn" Width="Auto"/>
					<ColumnDefinition Width="*"/>
				</Grid.ColumnDefinitions>
				<!-- Toolbox (docked) -->
				<ScrollViewer Grid.Column="0" Name="ToolBoxViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Visibility="Collapsed">
					<StackPanel Name="ToolBoxPanel" MinWidth="200" Margin="0,0,5,0" Background="{StaticResource ControlBackgroundBrush}" />
				</ScrollViewer>
				<!-- GridSplitter -->
				<GridSplitter Name="ToolBoxSplitter" Focusable="False" Width="2" Background="{StaticResource NormalBorderBrush}" VerticalAlignment="Stretch" HorizontalAlignment="Right" Visibility="Collapsed"/>
				<!-- Designer -->
				<ScrollViewer VirtualizingStackPanel.VirtualizationMode="Recycling" VirtualizingStackPanel.IsVirtualizing="True" Grid.Column="1" Margin="3,0,0,0" Name="DesignerScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" ScrollChanged="DesignerScrollViewer_ScrollChanged" Background="{StaticResource UnusedBackgroundBrush}">
					<ItemsControl x:Name="entityItems" PreviewMouseRightButtonDown="entityItems_Focus" PreviewMouseLeftButtonDown="entityItems_Focus" MouseEnter="entityItems_MouseEnter"  ItemsSource="{Binding Items, Mode=OneWay}">
						<ItemsControl.LayoutTransform>
							<TransformGroup>
								<ScaleTransform ScaleX="{Binding Source={x:Static Settings:UserSettingsHelper.Instance}, Path=ControlSize, Mode=TwoWay}"
					                          ScaleY="{Binding Source={x:Static Settings:UserSettingsHelper.Instance}, Path=ControlSize, Mode=TwoWay}"/>
							</TransformGroup>
						</ItemsControl.LayoutTransform>
						<ItemsControl.ContextMenu>
							<ContextMenu>
								<MenuItem Header="{Binding MenuLabelNewEntity}" Command="New"></MenuItem>
								<MenuItem Header="{Binding MenuLabelRemove}" Command="vm:CustomCommands.Close"></MenuItem>
								<MenuItem Header="{Binding MenuLabelDelete}" Command="Delete"></MenuItem>
							</ContextMenu>
						</ItemsControl.ContextMenu>
						<ItemsControl.ItemsPanel>
							<ItemsPanelTemplate>
								<lib:DesignerCanvas LostFocus="DesignerCanvas_LostFocus" GotFocus="DesignerCanvas_GotFocus" FocusManager.IsFocusScope="True" Focusable="True" IsEnabled="True" IsHitTestVisible="True" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" Background="{StaticResource DiagramBackgroundBrush}" MinHeight="800" MinWidth="1000" AllowDrop="True" Margin="4"/>
							</ItemsPanelTemplate>
						</ItemsControl.ItemsPanel>
						<ItemsControl.ItemContainerStyle>
							<Style>
								<Setter Property="Canvas.Left" Value="{Binding X, Mode=OneWay}"/>
								<Setter Property="Canvas.Top" Value="{Binding Y, Mode=OneWay}"/>
								<Setter Property="Canvas.Width" Value="{Binding Width, Mode=OneWay}"/>
								<Setter Property="Canvas.Height" Value="{Binding Height, Mode=OneWay}"/>
								<Setter Property="Canvas.ZIndex" Value="{Binding ZIndex, Mode=OneWay}"/>
							</Style>
						</ItemsControl.ItemContainerStyle>
					</ItemsControl>
				</ScrollViewer>
			</Grid>
			<Grid Name="UndockedGrid" lib:RoutedEventPlaceHolder.ElementOpened="Grid_ElementOpened" lib:RoutableDataGrid.ElementOpened="Grid_ElementOpened" ContextMenuOpening="Grid_ContextMenuOpening" ContextMenuClosing="Grid_ContextMenuClosing" MouseLeave="Grid_MouseLeave" VerticalAlignment="Top" HorizontalAlignment="Left">
				<Grid.ColumnDefinitions>
					<ColumnDefinition Width="Auto"/>
				</Grid.ColumnDefinitions>
				<!-- Toolbox (undocked) -->
				<ScrollViewer Grid.Row="0" Grid.Column="0" Name="ToolBoxUndockedViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Visibility="Collapsed" MouseLeave="ToolBoxUndockedViewer_MouseLeave">
					<StackPanel Name="ToolBoxUndockedPanel" MinWidth="200" Background="{StaticResource ControlBackgroundBrush}" />
				</ScrollViewer>
			</Grid>
		</Grid>
	</DockPanel>
</lib:EditControl>
